const app = getApp()
import {
  property_service_list,
  property_service_by_key,
  property_service_submit,
  comm_sql
} from '../../api/request'

Page({
  data: {
    room_code: '10-4-6',
    block: '10',
    mobile: '13883166466',
    property_service: '龙湖物业服务集团有限公司重庆分公司',
    input_onFocus: false, //搜索物业公司聚焦

    service_list: [], //按关键字查询物管公司
    property_list: [], //候选物管公司列表 
    room_code_list: [], //提名门牌号清单
    room_code_onFocus: false,
    cur_submit_index: '-1', //当前提名公司index
    property_select: '', //选择物管公司
  },

  search(e) {

    var data = {
      para: [e.detail]
    }
    property_service_by_key(data).then(res => {
      this.setData({
        input_onFocus: true,
        service_list: res.data,
      })
    })
  },

  onFocus() {
    this.setData({
      input_onFocus: true
    })
  },

  onLoad: function (options) {
    //    app.globalData.cur_property_code = '89'
    let that = this

    //聚焦框初始化    
    this.setData({
      input_onFocus: false,
      room_code_onFocus: false,
    })


    var data = {
      para: ["select mobile,property_service from building_room where property_code='" + app.globalData.cur_property_code + "' and room_code='" + app.globalData.cur_room_code + "'"]
    }
    comm_sql(data).then(res => {
      that.setData({
        mobile: res.data[0].MOBILE,
        property_service: res.data[0].PROPERTY_SERVICE,
      })

      var data = {
        para: [app.globalData.cur_property_code]
      }
      property_service_list(data).then(res => {
        that.setData({
          property_list: res.data,
        })
      })

    })
  },

  property_query_by_key(e) { //物业公司关键字查询
    let that = this
    var value = e.detail
    value != '' ? this.setData({
      vanLoading: true
    }) : ''

    var data = {
      para: [value]
    }
    PROPERTY_SERVICE_BY_KEY(data).then(res => {
      this.setData({
        property_list: res.data,
      })

    })
  },

  property_submit_list(e) {
    let that = this
    if (e.currentTarget.dataset.item == that.data.property_select && that.data.room_code_onFocus) {
      that.setData({
        room_code_onFocus: false,
      })
      return;
    }
    console.log('------------', e)
    var data = {
      para: ["select substr(room_code,1,length(room_code)-1) || '*' as room_code,submit_datetime from building_room where property_service='" + e.currentTarget.dataset.item + "'"]
    }
    comm_sql(data).then(res => {
      if (res.data.length == 0) {
        that.setData({
          room_code_onFocus: false,
        })

        wx.showModal({
          title: e.currentTarget.dataset.item,
          content: '该物管公司暂时没有业主提名'
        })
      } else
        console.log('-------', e.currentTarget)
      that.setData({
        room_code_onFocus: true,
        room_code_list: res.data,
        cur_submit_index: e.currentTarget.dataset.index,
        property_select: e.currentTarget.dataset.item,
      })
    })

  },

  property_select(e) {
    let that = this
    var data = {
      para: [app.globalData.cur_property_code,
        app.globalData.cur_room_code,
        e.currentTarget.dataset.item
      ]
    }
    property_service_submit(data).then(res => {
      that.onLoad()
      wx.showModal({
        title: '物管公司提名',
        content: res.data[0].ERRORMSG
      })
    })
  },

  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})